<?php
class media {
	var $CI;
	var $media_draft_model;
	public function __construct() {
		$this->CI = & get_instance ();
		$this->CI->load->model ( "media/media_draft_model" );
		$this->media_draft_model = $this->CI->media_draft_model;
	}
	public function save_media_draft($media, $urban_env, $explanation, $pic) {
		$check_result = $this->check_media ( $media );
		if ($check_result ['result'] == false) {
			return $check_result;
		}
		$pic_list = array ();
		if ($pic != "") {
			$pic_list = $pic;
		}
		for($i = 1; $i <= 9; $i ++) {
			if (isset ( $_FILES ['pic' . $i] ['tmp_name'] ) && is_uploaded_file ( $_FILES ['pic' . $i] ['tmp_name'] )) {
				$pic = $this->save_map ( 'pic' . $i );
				if ($pic ['result'] == false) {
					return $pic;
				}
				$this->create_media_thumb ( $pic ['upload_file'] );
				$pic_list [] = $pic ['upload_file'];
			}
		}
		
		if (isset ( $_FILES ['sketch_map'] ['tmp_name'] ) && is_uploaded_file ( $_FILES ['sketch_map'] ['tmp_name'] )) {
			$sketch_map = $this->save_map ( "sketch_map" );
			if ($sketch_map ['result'] == false) {
				return $sketch_map;
			}
			$media ['sketch_map'] = $sketch_map ['upload_file'];
		}
		if (isset ( $_FILES ['sketch_map1'] ['tmp_name'] ) && is_uploaded_file ( $_FILES ['sketch_map1'] ['tmp_name'] )) {
			$sketch_map = $this->save_map ( "sketch_map1" );
			if ($sketch_map ['result'] == false) {
				return $sketch_map;
			}
			$media ['sketch_map'] = $sketch_map ['upload_file'];
		}
		
		$media ['specification'] = $this->calculate_specification ( $media ['specification_width'], $media ['specification_height'] );
		$media ['media_status'] = 0;
		
		if (! $media ['unique_id']) {
			$media ['unique_id'] = uniqid ( md5 ( rand () ), true );
		}
		
		if ($media ['play_format'] != "" && is_array ( $media ['play_format'] )) {
			$media ['play_format'] = implode ( ",", $media ['play_format'] );
		}
		
		$media_draft_id = $this->media_draft_model->save_media_draft ( $media );
		
		$org_media_draft = "";
		if ($media ['quote'] == "0" || $media ['quote'] == "") {
			$action_type = "create";
		} else {
			$org_media_draft = $this->query_media ( $media ['quote'] );
			if ($org_media_draft == null) {
				$result ['result'] = false;
				$result ['message'] = "原始媒体没有找到," . $media ['quote'];
				return $result;
			}
			
			$action_type = "re_create";
		}
		
		if ($media ['media_type'] == "led" || $media ['media_type'] == "common") {
			$this->update_draft_env ( $urban_env, $media_draft_id );
		}
		$this->update_media_draft_pic ( $pic_list, $media_draft_id );
		
		$this->CI->load->library ( "media/media_draft_log" );
		$this->CI->media_draft_log->insert_log ( $media_draft_id, $action_type, $media ['create_by'], $media ['create_time'], $explanation, $org_media_draft, $media );
		$result ['result'] = true;
		$result ['message'] = "添加媒体草稿成功";
		$result ['media_draft_id'] = $media_draft_id;
		$result ['redirect_url'] = base_url () . "index.php/media_media_manage";
		return $result;
	}
	private function calculate_specification($width, $height) {
		$width_list = explode ( "+", $width );
		$height_list = explode ( "+", $height );
		$specification = 0;
		for($i = 0; $i < count ( $width_list ); $i ++) {
			$specification = $specification + (floatval ( $width_list [$i] ) * floatval ( $height_list [$i] ));
		}
		return $specification;
	}
	public function create_media_thumb($img) {
		$config ['image_library'] = 'gd2';
		$config ['source_image'] = $this->CI->config->item ( 'ssi_file_path' ) . $img;
		$config ['create_thumb'] = TRUE;
		$config ['maintain_ratio'] = TRUE;
		$config ['width'] = 200;
		$config ['height'] = 150;
		
		$this->CI->load->library ( 'image_lib', $config );
		$this->CI->image_lib->resize ();
	}
	public function check_media($media, $media_id = "") {
		$result ['result'] = true;
		$result ['message'] = "媒体信息填写正确";
		if ($media ['media_type'] == "led" || $media ['media_type'] == "common") {
			if ($media ['region'] == "") {
				$result ['result'] = false;
				$result ['message'] = "请选择媒体所在区域";
				return $result;
			}
		}
		if ($media ['media_type'] == "airport" || $media ['media_type'] == "airport_led") {
			if ($media ['airport_id'] == "") {
				$result ['result'] = false;
				$result ['message'] = "请选择媒体所在的机场";
				return $result;
			}
		}
		if ($media ['media_type'] == "highway") {
			if ($media ['highway_id'] == "") {
				$result ['result'] = false;
				$result ['message'] = "请选择媒体所在的高速公路";
				return $result;
			}
		}
		if ($media ['location'] == "") {
			$result ['result'] = false;
			$result ['message'] = "请填写媒体位置";
			return $result;
		}
		return $result;
	}
	public function save_map($input_file_name) {
		$this->CI->load->library ( "tools/ssi_upload_file" );
		return $this->CI->ssi_upload_file->upload_pic ( $input_file_name );
	}
	private function update_draft_env($urban_env, $media_draft_id) {
		$this->CI->load->model ( "media/media_draft_env_model" );
		$this->CI->media_draft_env_model->delete_media_draft_env ( $media_draft_id );
		foreach ( $urban_env as $k => $v ) {
			if ($v != "") {
				$this->CI->media_draft_env_model->save_media_draft_env ( $k, $v, $media_draft_id );
			}
		}
	}
	private function update_media_draft_pic($pic_list, $media_draft_id) {
		$this->media_draft_model->delete_pic ( $media_draft_id );
		if ($pic_list != null) {
			foreach ( $pic_list as $pic ) {
				$path_info = pathinfo ( $pic );
				$pic_thumb = $path_info ['dirname'] . "/" . $path_info ['filename'] . "_thumb." . $path_info ['extension'];
				$this->media_draft_model->save_pic ( $pic, $pic_thumb, $media_draft_id );
			}
		}
	}
	public function add_media_draft_pic($pic, $media_draft_id) {
		$path_info = pathinfo ( $pic );
		$pic_thumb = $path_info ['dirname'] . "/" . $path_info ['filename'] . "_thumb." . $path_info ['extension'];
		$media ['media_status'] = 0;
		$this->media_draft_model->update_media_draft ( $media, $media_draft_id );
		$this->media_draft_model->save_pic ( $pic, $pic_thumb, $media_draft_id );
	}
	public function save_media_draft_pic($media_draft_id) {
		$dat = file_get_contents ( "php://input" );
		$dat = explode ( "data:image/jpeg;base64,", $dat );
		$base_path = $this->CI->config->item ( 'ssi_file_path' );
		$date_path = "/" . date ( "Y" );
		if (! is_dir ( $base_path . $date_path )) {
			mkdir ( $base_path . $date_path, 0777 );
		}
		$date_path = $date_path . "/" . date ( "m" );
		if (! is_dir ( $base_path . $date_path )) {
			mkdir ( $base_path . $date_path, 0777 );
		}
		$date_path = $date_path . "/" . date ( "d" ) . "/";
		if (! is_dir ( $base_path . $date_path )) {
			mkdir ( $base_path . $date_path, 0777 );
		}
		$path = $base_path . $date_path;
		$this->media_draft_model->delete_pic ( $media_draft_id );
		foreach ( $dat as $d ) {
			if (strlen ( $d ) > 1000) {
				$d = base64_decode ( $d );
				$name = md5 ( $d );
				$url = $path . $name . '.jpg';
				file_put_contents ( $url, $d );
				$img_path = $date_path . $name . '.jpg';
				$this->create_media_thumb ( $img_path );
				$this->media_draft_model->save_pic ( $img_path, $date_path . $name . '_thumb.jpg', $media_draft_id );
			}
		}
	}
	public function update_media_draft_sketch_map($media, $media_draft_id) {
		$this->media_draft_model->update_media_draft ( $media, $media_draft_id );
	}
	public function update_media_draft($media, $urban_env, $explanation, $pic, $media_draft_id, $update_pic = 1) {
		$check_result = $this->check_media ( $media );
		if ($check_result ['result'] == false) {
			return $check_result;
		}
		$pic_list = array ();
		if ($pic != "") {
			$pic_list = $pic;
		}
		for($i = 1; $i <= 9; $i ++) {
			if (isset ( $_FILES ['pic' . $i] ['tmp_name'] ) && is_uploaded_file ( $_FILES ['pic' . $i] ['tmp_name'] )) {
				$pic = $this->save_map ( 'pic' . $i );
				if ($pic ['result'] == false) {
					return $pic;
				}
				$this->create_media_thumb ( $pic ['upload_file'] );
				$pic_list [] = $pic ['upload_file'];
			}
		}
		
		if ($media ['play_format'] != "" && is_array ( $media ['play_format'] )) {
			$media ['play_format'] = implode ( ",", $media ['play_format'] );
		}
		if (isset ( $_FILES ['sketch_map1'] ['tmp_name'] ) && is_uploaded_file ( $_FILES ['sketch_map1'] ['tmp_name'] )) {
			$sketch_map = $this->save_map ( "sketch_map1" );
			if ($sketch_map ['result'] == false) {
				return $sketch_map;
			}
			$media ['sketch_map'] = $sketch_map ['upload_file'];
		}
		$media ['specification'] = $this->calculate_specification ( $media ['specification_width'], $media ['specification_height'] );
		
		// 插入日志
		$org_media_draft = $this->query_media_draft ( $media_draft_id );
		$this->CI->load->library ( "media/media_draft_log" );
		$this->CI->media_draft_log->insert_log ( $media_draft_id, 'update', $media ['modify_by'], $media ['modify_time'], $explanation, $media, $org_media_draft );
		
		$media ['media_status'] = 0;
		$this->media_draft_model->update_media_draft ( $media, $media_draft_id );
		
		if ($media ['media_type'] == "led" || $media ['media_type'] == "common") {
			$this->update_draft_env ( $urban_env, $media_draft_id );
		}
		// 判断是否是手机端传递的的借口,要不要删除之前的图片
		if ($update_pic) {
			$this->update_media_draft_pic ( $pic_list, $media_draft_id );
		}
		$result ['result'] = true;
		$result ['media_draft_id'] = $media_draft_id;
		$result ['message'] = "编辑媒体草稿成功";
		$result ['redirect_url'] = base_url () . "index.php/media_media_manage";
		return $result;
	}
	public function query_my_media_draft_list($user_name, $user_id, $search) {
		$start = intval ( $this->CI->input->get ( "per_page" ) );
		$limit = $this->CI->config->item ( "page_size" );
		return $this->media_draft_model->query_my_media_draft_list ( $user_name, $user_id, $start, $limit, $search );
	}
	public function query_my_media_draft_num($user_name, $user_id, $search) {
		return $this->media_draft_model->query_my_media_draft_num ( $user_name, $user_id, $search );
	}
	public function query_media_draft($media_draft_id) {
		return $this->media_draft_model->query_media_draft ( $media_draft_id );
	}
	public function query_media_draft_detail($media_draft_id) {
		return $this->media_draft_model->query_media_draft_detail ( $media_draft_id );
	}
	public function query_media_draft_pic_list($media_draft_id) {
		return $this->media_draft_model->query_media_draft_pic_list ( $media_draft_id );
	}
	public function query_media_draft_env_list($media_draft_id) {
		$this->CI->load->model ( "media/media_draft_env_model" );
		return $this->CI->media_draft_env_model->query_media_draft_env_list ( $media_draft_id );
	}
	public function delete_media_draft($media_draft_id) {
		$this->media_draft_model->delete_media_draft ( $media_draft_id );
		$this->media_draft_model->delete_pic ( $media_draft_id );
		$this->CI->load->model ( "media/media_draft_env_model" );
		$this->CI->media_draft_env_model->delete_media_draft_env ( $media_draft_id );
	}
	public function delete_media($media_id) {
		$this->CI->load->model ( "media/media_model" );
		$this->CI->media_model->delete_media ( $media_id );
	}
	public function submit_check_draft($media_draft_id, $user_name = "") {
		if ($user_name == "") {
			$user_name = $_SESSION ['user_name'];
		}
		$affected_rows = $this->media_draft_model->submit_check_draft ( $media_draft_id );
		if ($affected_rows) {
			$this->CI->load->library ( "media/media_draft_log" );
			$this->CI->media_draft_log->insert_log ( $media_draft_id, 'submit_check', $user_name, date ( "Y-m-d H:i:s" ), '' );
		}
	}
	public function query_check_media_draft_list($user_id, $search) {
		$start = intval ( $this->CI->input->get ( "per_page" ) );
		$limit = $this->CI->config->item ( "page_size" );
		return $this->media_draft_model->query_check_media_draft_list ( $user_id, $start, $limit, $search );
	}
	public function query_check_media_draft_num($user_id, $search) {
		return $this->media_draft_model->query_check_media_draft_num ( $user_id, $search );
	}
	public function allow_media($media_draft_id) {
		$media_draft = $this->query_media_draft ( $media_draft_id );
		if ($media_draft ['media_status'] == 10) {
			
			$media ['media_type'] = $media_draft ['media_type'];
			$media ['province'] = $media_draft ['province'];
			$media ['city'] = $media_draft ['city'];
			$media ['region'] = $media_draft ['region'];
			$media ['location'] = $media_draft ['location'];
			$media ['vendor'] = $media_draft ['vendor'];
			$media ['media_type_id'] = $media_draft ['media_type_id'];
			$media ['crafts'] = $media_draft ['crafts'];
			$media ['corner_case'] = $media_draft ['corner_case'];
			$media ['illumination'] = $media_draft ['illumination'];
			$media ['flow'] = $media_draft ['flow'];
			$media ['media_area'] = $media_draft ['media_area'];
			$media ['reference_price'] = $media_draft ['reference_price'];
			$media ['sketch_map'] = $media_draft ['sketch_map'];
			$media ['longitude'] = $media_draft ['longitude'];
			$media ['latitude'] = $media_draft ['latitude'];
			$media ['specification_width'] = $media_draft ['specification_width'];
			$media ['specification_height'] = $media_draft ['specification_height'];
			$media ['specification'] = $media_draft ['specification'];
			
			$media ['modify_by'] = $_SESSION ['user_name'];
			$media ['modify_time'] = date ( "Y-m-d H:i:s" );
			
			$media ['highway_id'] = $media_draft ['highway_id'];
			$media ['section_id'] = $media_draft ['section_id'];
			$media ['point_id'] = $media_draft ['point_id'];
			$media ['airport_id'] = $media_draft ['airport_id'];
			
			$media ['modify_time'] = date ( "Y-m-d H:i:s" );
			$media ['modify_by'] = $_SESSION ['user_name'];
			
			$media ['unique_id'] = $media_draft ['unique_id'];
			
			$media ['agreement'] = $media ['agreement'];
			$media ['price_remark'] = $media ['price_remark'];
			
			$media ['play_time'] = $media ['play_time'];
			$media ['play_time_remark'] = $media ['play_time_remark'];
			$media ['play_format'] = $media ['play_format'];
			$media ['play_frequency'] = $media ['play_frequency'];
			$media ['play_frequency_remark'] = $media ['play_frequency_remark'];
			$media ['resolution'] = $media ['resolution'];
			
			$this->CI->load->model ( "media/media_model" );
			if ($media_draft ['quote'] == "0" || $media_draft ['quote'] == "") {
				$media ['create_time'] = date ( "Y-m-d H:i:s" );
				$media ['create_by'] = $_SESSION ['user_name'];
				
				$media_id = $this->CI->media_model->save_media ( $media );
				$this->update_media_env ( $media_draft_id, $media_id, $media, true );
			} else {
				$media_id = $media_draft ['quote'];
				$this->CI->media_model->update_media ( $media, $media_draft ['quote'] );
				$this->update_media_env ( $media_draft_id, $media_id, $media, false );
			}
			
			$this->update_media_pic ( $media_draft_id, $media_id );
			$affected_rows = $this->media_draft_model->set_delete_media_draft ( $media_draft_id );
			if ($affected_rows) {
				$this->CI->load->library ( "media/media_draft_log" );
				$this->CI->media_draft_log->insert_log ( $media_draft_id, 'allow', $_SESSION ['user_name'], date ( "Y-m-d H:i:s" ), '' );
			}
		}
	}
	public function update_media_env($media_draft_id, $media_id, $media, $add_num = false) {
		$media_env_list = $this->query_media_draft_env_list ( $media_draft_id );
		$this->CI->load->model ( "media/media_model" );
		$this->CI->media_model->delete_media_env ( $media_id );
		if ($media_env_list != null) {
			foreach ( $media_env_list as $media_env ) {
				$this->CI->media_model->save_media_env ( $media_env ['env_type'], $media_env ['env_id'], $media_id );
				if ($add_num) {
					$this->CI->media_model->add_num ( $media_env ['env_type'], $media_env ['env_id'], $media ['media_type'] );
				}
			}
		}
		if ($add_num) {
			$this->CI->media_model->add_env_num ( $media ['highway_id'], "highway" );
			$this->CI->media_model->add_env_num ( $media ['section_id'], "section" );
			$this->CI->media_model->add_env_num ( $media ['point_id'], "point" );
			$this->CI->media_model->add_env_num ( $media ['airport_id'], "airport" );
		}
	}
	public function update_media_pic($media_draft_id, $media_id) {
		$media_pic_list = $this->query_media_draft_pic_list ( $media_draft_id );
		$this->CI->load->model ( "media/media_model" );
		$this->CI->media_model->delete_media_pic ( $media_id );
		if ($media_pic_list != null) {
			foreach ( $media_pic_list as $media_pic ) {
				$this->CI->media_model->save_pic ( $media_pic ['pic'], $media_pic ['pic_thumb'], $media_id );
			}
		}
	}
	public function not_allow_draft($media_draft_id, $explanation) {
		$affected_rows = $this->media_draft_model->not_allow_draft ( $media_draft_id );
		if ($affected_rows) {
			$this->CI->load->library ( "media/media_draft_log" );
			$this->CI->media_draft_log->insert_log ( $media_draft_id, 'not_allow', $_SESSION ['user_name'], date ( "Y-m-d H:i:s" ), $explanation );
		}
	}
	public function query_media_list($user_id, $search) {
		$start = intval ( $this->CI->input->get ( "per_page" ) );
		$limit = $this->CI->config->item ( "page_size" );
		$this->CI->load->model ( "media/media_model" );
		return $this->CI->media_model->query_media_list ( $user_id, $start, $limit, $search );
	}
	public function query_media_num($user_id, $search) {
		$this->CI->load->model ( "media/media_model" );
		return $this->CI->media_model->query_media_num ( $user_id, $search );
	}
	public function query_media($media_id) {
		$this->CI->load->model ( "media/media_model" );
		return $this->CI->media_model->query_media ( $media_id );
	}
	public function query_media_detail($media_id) {
		$this->CI->load->model ( "media/media_model" );
		return $this->CI->media_model->query_media_detail ( $media_id );
	}
	public function query_media_pic_list($media_id) {
		$this->CI->load->model ( "media/media_model" );
		return $this->CI->media_model->query_media_pic_list ( $media_id );
	}
	public function query_media_env_list($media_id) {
		$this->CI->load->model ( "media/media_model" );
		return $this->CI->media_model->query_media_env_list ( $media_id );
	}
	public function query_env_media_list($env_id, $env_type, $media_type) {
		$this->CI->load->model ( "media/media_model" );
		return $this->CI->media_model->query_env_media_list ( $env_id, $env_type, $media_type );
	}
	public function save_assgin_excellent_media($env_id, $env_type, $common_media, $led_media) {
		$this->CI->load->model ( "media/media_model" );
		$this->CI->media_model->delete_excellent_media ( $env_id, $env_type );
		if ($common_media != null) {
			foreach ( $common_media as $common ) {
				$this->CI->media_model->save_assgin_excellent_media ( $env_id, $env_type, $common, "common" );
			}
		}
		if ($led_media != null) {
			foreach ( $led_media as $led ) {
				$this->CI->media_model->save_assgin_excellent_media ( $env_id, $env_type, $led, "led" );
			}
		}
	}
	public function query_env_excellent_media_list($env_id, $env_type, $media_type) {
		$this->CI->load->model ( "media/media_model" );
		return $this->CI->media_model->query_env_excellent_media_list ( $env_id, $env_type, $media_type );
	}
	public function mobile_query_my_media_draft_list($user_name, $user_id, $sort) {
		$start = intval ( $this->CI->input->get ( "per_page" ) );
		$limit = $this->CI->config->item ( "page_size" );
		$this->CI->load->model ( "media/media_draft_model" );
		return $this->CI->media_draft_model->mobile_query_my_media_draft_list ( $user_name, $user_id, $sort, $start, $limit );
	}
	public function mobile_search_my_media_draft_list($user_name, $user_id, $sort, $keyword) {
		$this->CI->load->model ( "media/media_draft_model" );
		return $this->CI->media_draft_model->mobile_search_my_media_draft_list ( $user_name, $user_id, $sort, $keyword );
	}
	public function mobile_query_nearby_media($user_id, $longitude, $latitude) {
		$this->CI->load->model ( "media/media_model" );
		return $this->CI->media_model->mobile_query_nearby_media ( $user_id, $longitude, $latitude );
	}
	public function search_my_media_list($user_name, $user_id, $keyword) {
		$this->CI->load->model ( "media/media_model" );
		return $this->CI->media_model->search_my_media_list ( $user_name, $user_id, $keyword );
	}
	public function delete_draft_pic($pic_id) {
		$this->CI->load->model ( "media/media_draft_model" );
		return $this->CI->media_draft_model->delete_draft_pic ( $pic_id );
	}
	public function import_media_draft($media_type) {
		$base_path = $this->CI->config->item ( 'ssi_file_path' );
		$temporary_path = "/temporary";
		if (! is_dir ( $base_path . $temporary_path )) {
			mkdir ( $base_path . $temporary_path, 0777 );
		}
		$config ['upload_path'] = $base_path . $temporary_path;
		$config ['allowed_types'] = '*';
		$config ['max_size'] = '20000'; // 20M
		$config ['encrypt_name'] = true;
		if (! is_dir ( $config ['upload_path'] )) {
			mkdir ( $config ['upload_path'], 0777 );
		}
		
		$this->CI->load->library ( 'upload', $config );
		if (! $this->CI->upload->do_upload ( "excel" )) {
			$data ['result'] = false;
			$data ['message'] = $this->CI->upload->display_errors ( '', '' );
			return $data;
		}
		$upload_info = $this->CI->upload->data ();
		if ($upload_info ['file_ext'] != ".xls" && $upload_info ['file_ext'] != ".xlsx") {
			$data ['result'] = false;
			$data ['message'] = "您上传的必须是一个excel文件";
			return $data;
		}
		$insert_message = $this->add_import_media_draft_list ( $upload_info, $media_type );
		return $insert_message;
	}
	private function add_import_media_draft_list($file_info, $media_type) {
		$this->CI->load->library ( "tools/PHPExcel" );
		$objReader = new Exception ();
		if (strtolower ( $file_info ['file_ext'] ) == '.xls') {
			$objReader = PHPExcel_IOFactory::createReader ( 'Excel5' );
		} else {
			$objReader = PHPExcel_IOFactory::createReader ( 'Excel2007' );
		}
		ini_set ( 'memory_limit', '256M' );
		if (! $objReader->canRead ( $file_info ['full_path'] )) {
			$result ['result'] = false;
			$result ['message'] = "excel文件读取错误,请重新编辑后再试.";
			return $result;
		}
		$objPHPExcel = $objReader->load ( $file_info ['full_path'] );
		$sheet = $objPHPExcel->getSheet ( 0 );
		if ($sheet->getHighestRow () == 0) {
			$result ['result'] = false;
			$result ['message'] = "excel中的内容是空的.";
			return $result;
		}
		
		$this->CI->load->library ( "sys/region" );
		$this->CI->load->library ( "ven/vendor" );
		$this->CI->load->library ( "sys/corner_case" );
		$this->CI->load->library ( "sys/crafts" );
		$this->CI->load->library ( "sys/media_type" );
		
		$total_row = $sheet->getHighestRow ();
		$insert_num = 0;
		for($i = 1; $i <= $sheet->getHighestRow (); $i ++) {
			if (trim ( $objPHPExcel->getActiveSheet ()->getCell ( "A" . $i )->getValue () ) != "") {
				$media_draft = array (
						"col1" => trim ( $objPHPExcel->getActiveSheet ()->getCell ( "A" . $i )->getValue () ),
						"col2" => trim ( $objPHPExcel->getActiveSheet ()->getCell ( "B" . $i )->getValue () ),
						"col3" => trim ( $objPHPExcel->getActiveSheet ()->getCell ( "C" . $i )->getValue () ),
						"col4" => trim ( $objPHPExcel->getActiveSheet ()->getCell ( "D" . $i )->getValue () ),
						"col5" => trim ( $objPHPExcel->getActiveSheet ()->getCell ( "E" . $i )->getValue () ),
						"col6" => trim ( $objPHPExcel->getActiveSheet ()->getCell ( "F" . $i )->getValue () ),
						"col7" => trim ( $objPHPExcel->getActiveSheet ()->getCell ( "G" . $i )->getValue () ),
						"col8" => trim ( $objPHPExcel->getActiveSheet ()->getCell ( "H" . $i )->getValue () ),
						"col9" => trim ( $objPHPExcel->getActiveSheet ()->getCell ( "I" . $i )->getValue () ),
						"col10" => trim ( $objPHPExcel->getActiveSheet ()->getCell ( "J" . $i )->getValue () ),
						"col11" => trim ( $objPHPExcel->getActiveSheet ()->getCell ( "K" . $i )->getValue () ),
						"col12" => trim ( $objPHPExcel->getActiveSheet ()->getCell ( "L" . $i )->getValue () ),
						"col13" => trim ( $objPHPExcel->getActiveSheet ()->getCell ( "M" . $i )->getValue () ),
						"col14" => trim ( $objPHPExcel->getActiveSheet ()->getCell ( "N" . $i )->getValue () ),
						"col15" => trim ( $objPHPExcel->getActiveSheet ()->getCell ( "O" . $i )->getValue () ),
						"col16" => trim ( $objPHPExcel->getActiveSheet ()->getCell ( "P" . $i )->getValue () ),
						"col17" => trim ( $objPHPExcel->getActiveSheet ()->getCell ( "Q" . $i )->getValue () ),
						"col18" => trim ( $objPHPExcel->getActiveSheet ()->getCell ( "R" . $i )->getValue () ),
						"col19" => trim ( $objPHPExcel->getActiveSheet ()->getCell ( "S" . $i )->getValue () ),
						"col20" => trim ( $objPHPExcel->getActiveSheet ()->getCell ( "T" . $i )->getValue () ),
						"col21" => trim ( $objPHPExcel->getActiveSheet ()->getCell ( "U" . $i )->getValue () ),
						"col22" => trim ( $objPHPExcel->getActiveSheet ()->getCell ( "V" . $i )->getValue () ),
						"col23" => trim ( $objPHPExcel->getActiveSheet ()->getCell ( "W" . $i )->getValue () ),
						"col24" => trim ( $objPHPExcel->getActiveSheet ()->getCell ( "X" . $i )->getValue () ),
						"col25" => trim ( $objPHPExcel->getActiveSheet ()->getCell ( "Y" . $i )->getValue () ),
						"col26" => trim ( $objPHPExcel->getActiveSheet ()->getCell ( "Z" . $i )->getValue () ) 
				);
				$insert_num = $insert_num + 1;
				if ($media_type == "highway") {
					
					$media = array ();
					// 判断是否是标题列
					if ($media_draft ['col1'] == "省份") {
						continue;
					}
					$media ['media_type'] = "highway";
					$province_info = $this->CI->region->get_region_info_by_name ( trim ( $media_draft ['col1'] ) );
					$media ['province'] = $province_info ['region_id'];
					$media ['location'] = $media_draft ['col6'];
					$vendor_info = $this->CI->vendor->query_vendor_by_name ( trim ( $media_draft ['col4'] ) );
					$media ['vendor'] = $vendor_info ['vendor_id'];
					$media_type_info = $this->CI->media_type->get_media_type_info_by_name ( trim ( $media_draft ['col10'] ) );
					$media ['media_type_id'] = $media_type_info ['media_type_id'];
					$crafts_info = $this->CI->crafts->get_crafts_info_by_name ( trim ( $media_draft ['col13'] ) );
					$media ['crafts'] = $crafts_info ['crafts_id'];
					$corner_case_info = $this->CI->corner_case->get_corner_case_info_by_name ( trim ( $media_draft ['col11'] ) );
					$media ['corner_case'] = $corner_case_info ['corner_case_id'];
					$media ['illumination'] = $media_draft ['col12'] == "Y" ? "1" : "0";
					$media ['reference_price'] = $media_draft ['col15'];
					$media ['specification_width'] = $media_draft ['col7'];
					$media ['specification_height'] = $media_draft ['col8'];
					$media ['specification'] = $media_draft ['col9'];
					$media ['quote'] = "";
					$media ['unique_id'] = uniqid ( md5 ( rand () ), true );
					$media ['agreement'] = get_agreement_id ( $media_draft ['col23'] );
					$media ['price_remark'] = $media_draft ['col24'];
					
					$media ['create_time'] = date ( "Y-m-d H:i:s" );
					$media_draft_id = $this->media_draft_model->save_media_draft ( $media );
					if ($media_draft ['col25']) {
						$this->media_draft_model->save_pic ( $media_draft ['col25'], "", $media_draft_id );
					}
					if ($media_draft ['col26']) {
						$this->media_draft_model->save_pic ( $media_draft ['col26'], "", $media_draft_id );
					}
				} else {
					$media = array ();
					// 判断是否是标题列
					if ($media_draft ['col1'] == "省份") {
						continue;
					}
					if (trim ( $media_draft ['col10'] ) == "LED显示屏") {
						$media ['media_type'] = "led";
					} else if(strstr(  $media_draft ['col6'] ,"机场")||strstr(  $media_draft ['col6'] ,"航站楼")) {
						$media ['media_type'] = "airport";
					} else {
						$media ['media_type'] = "common";
					}
					$province_info = $this->CI->region->get_region_info_by_name ( trim ( $media_draft ['col1'] ) );
					$media ['province'] = $province_info ['region_id'];
					$media ['location'] = $media_draft ['col6'];
					$vendor_info = $this->CI->vendor->query_vendor_by_name ( trim ( $media_draft ['col4'] ) );
					$media ['vendor'] = $vendor_info ['vendor_id'];
					$media_type_info = $this->CI->media_type->get_media_type_info_by_name ( trim ( $media_draft ['col10'] ) );
					$media ['media_type_id'] = $media_type_info ['media_type_id'];
					$crafts_info = $this->CI->crafts->get_crafts_info_by_name ( trim ( $media_draft ['col13'] ) );
					$media ['crafts'] = $crafts_info ['crafts_id'];
					$corner_case_info = $this->CI->corner_case->get_corner_case_info_by_name ( trim ( $media_draft ['col11'] ) );
					$media ['corner_case'] = $corner_case_info ['corner_case_id'];
					$media ['illumination'] = $media_draft ['col12'] == "Y" ? "1" : "0";
					$media ['reference_price'] = $media_draft ['col15'];
					$media ['specification_width'] = $media_draft ['col7'];
					$media ['specification_height'] = $media_draft ['col8'];
					$media ['specification'] = $media_draft ['col9'];
					$media ['quote'] = "";
					$media ['unique_id'] = uniqid ( md5 ( rand () ), true );
					$media ['agreement'] = get_agreement_id ( $media_draft ['col23'] );
					$media ['price_remark'] = $media_draft ['col24'];
					
					$media ['create_time'] = date ( "Y-m-d H:i:s" );
					$media_draft_id = $this->media_draft_model->save_media_draft ( $media );
					if ($media_draft ['col25']) {
						$this->media_draft_model->save_pic ( $media_draft ['col25'], "", $media_draft_id );
					}
					if ($media_draft ['col26']) {
						$this->media_draft_model->save_pic ( $media_draft ['col26'], "", $media_draft_id );
					}
				}
			}
		}
		$result ['result'] = true;
		$result ['message'] = "共导入了 <b>" . $insert_num . "</b> 条媒体草稿";
		return $result;
	}
}